package io.renren.modules.sys.service.impl;

import io.renren.modules.sys.dao.SysUserRoleDao;
import io.renren.modules.sys.entity.*;
import io.renren.modules.sys.service.SysRoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.ShopOrderDao;
import io.renren.modules.sys.service.ShopOrderService;


@Service("shopOrderService")
public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderDao, ShopOrderEntity> implements ShopOrderService {

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    private SysRoleService sysRoleService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数
        String orderSn = null;
        if( params.get("orderSn")!=null&&!params.get("orderSn").equals("")){//设备编号
            orderSn =params.get("orderSn").toString();
        }
        Integer status = null;
        if( params.get("status")!=null&&!params.get("status").equals("")){//二维码编号id
            status =Integer.parseInt(params.get("status").toString());
        }

        Long roleId = sysUserRoleDao.queryRoleId(user.getUserId());
        Long userid = null;
        if(roleId!=null) {
            SysRoleEntity role = sysRoleService.getById(roleId);
            if (role.getRoleName().indexOf("代理商") != -1||
                    role.getRoleName().indexOf("业务员") != -1||role.getRoleName().indexOf("商户") != -1) {//当前登陆用户不为管理员
                userid = user.getUserId();

            }
        }
        int count = baseMapper.queryListcount(orderSn,status,userid);
        List<ShopOrderVOEntity> list = baseMapper.getOrder(orderSn,status,userid,(currPage-1)*pageSize,currPage*pageSize);
        return new PageUtils(list,count,pageSize,currPage);
    }

    @Override
    public List<ShopOrderEntity> queryList(){
        return baseMapper.queryList();
    }


    @Override
    public List<ShopOrderVOEntity> getOrderMoney(){
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        Long roleId = sysUserRoleDao.queryRoleId(user.getUserId());
        Long userid = null;
        if(roleId!=null) {
            SysRoleEntity role = sysRoleService.getById(roleId);
            if (role.getRoleName().indexOf("代理商") != -1||
                    role.getRoleName().indexOf("业务员") != -1||role.getRoleName().indexOf("商户") != -1) {//当前登陆用户不为管理员
                userid = user.getUserId();

            }
        }
        List<ShopOrderVOEntity> entity = baseMapper.getOrderMoney(userid);
        return  entity;
    }

    @Override
    public PageUtils queryAgentPage(Map<String, Object> params){
        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数
        String orderSn = null;
        if( params.get("orderSn")!=null&&!params.get("orderSn").equals("")){//设备编号
            orderSn =params.get("orderSn").toString();
        }
        Long userid = null;
        if( params.get("userid")!=null&&!params.get("userid").equals("")){//
            userid =Long.valueOf(params.get("userid").toString());
        }
        int count = baseMapper.queryAgentcount(orderSn,userid);
        List<ShopOrderVOEntity> list = baseMapper.getOrderByAgent(orderSn,userid,(currPage-1)*pageSize,currPage*pageSize);
        return new PageUtils(list,count,pageSize,currPage);
    }

    @Override
    public PageUtils querySalePage(Map<String, Object> params){
        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数
        String orderSn = null;
        if( params.get("orderSn")!=null&&!params.get("orderSn").equals("")){//设备编号
            orderSn =params.get("orderSn").toString();
        }
        Long userid = null;
        if( params.get("userid")!=null&&!params.get("userid").equals("")){//
            userid =Long.valueOf(params.get("userid").toString());
        }
        int count = baseMapper.querySalecount(orderSn,userid);
        List<ShopOrderVOEntity> list = baseMapper.getOrderBySale(orderSn,userid,(currPage-1)*pageSize,currPage*pageSize);
        return new PageUtils(list,count,pageSize,currPage);
    }

    @Override
    public PageUtils queryMerchantPage(Map<String, Object> params){
        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数
        String orderSn = null;
        if( params.get("orderSn")!=null&&!params.get("orderSn").equals("")){//设备编号
            orderSn =params.get("orderSn").toString();
        }
        Long userid = null;
        if( params.get("userid")!=null&&!params.get("userid").equals("")){//
            userid =Long.valueOf(params.get("userid").toString());
        }
        int count = baseMapper.queryMerchantcount(orderSn,userid);
        List<ShopOrderVOEntity> list = baseMapper.getOrderByMerchant(orderSn,userid,(currPage-1)*pageSize,currPage*pageSize);
        return new PageUtils(list,count,pageSize,currPage);
    }
}
